package com.example.module_motocircle.adpater

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.TypedValue
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.example.lib_common.base.utils.ToastUtils
import com.example.lib_common.dp
import com.example.lib_image.GlideApp
import com.example.module_motocircle.R
import com.example.module_motocircle.model.Image


/**
 *@author Wcj
 *@description
 *@date 2022/7/5 15:42
 */
class NinePicsAdapter(private val mPicList:List<Image>, private val imageClick:(Int)->Unit ):RecyclerView.Adapter<NinePicsAdapter.NinePicsViewHolder>() {


    /**
     * 多布局
     */
    override fun getItemViewType(position: Int): Int {
        return if(mPicList.isEmpty()){
            NONE.value
        }else if (mPicList.size == 1){
            val image = mPicList[position]
            if (image.imageWidth > image.imageHeight){
                LANDSCAPE.value
            }else{
                PORTRAIT.value
            }
        }else{
            NINE.value
        }

    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NinePicsViewHolder {
        val imageView = ImageView(parent.context).apply {
            scaleType = ImageView.ScaleType.CENTER_CROP //
            background = context.getDrawable(R.drawable.shape_bg_mask_round_4dp) //背景
            foreground = context.getDrawable(R.drawable.shape_bg_normal_ripple_with_round_mask) //前景
        }
        return when(viewType){
            NINE.value -> NinePicsViewHolder(imageView, 110.dp,110.dp)
            LANDSCAPE.value -> NinePicsViewHolder(imageView,230.dp,170.dp)
            PORTRAIT.value -> NinePicsViewHolder(imageView,230.dp,310.dp)
            else -> NinePicsViewHolder(imageView,110.dp,110.dp)
        }

    }

    override fun getItemCount(): Int {
        return mPicList.size
    }

    override fun onBindViewHolder(holder: NinePicsViewHolder, position: Int) {
        val imageItem = mPicList[position]
        val isVideo = imageItem.contentType?.contains("video") //判断是否为视频
        val thumbUrl = if(isVideo!!){ //图片
            imageItem.firstImage
        }else{
            imageItem.url
        }
        //展示图片
        GlideApp.with(holder.itemView.context)
            .load(thumbUrl)
            .listener(object :RequestListener<Drawable>{
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>,
                    isFirstResource: Boolean
                ): Boolean {
                    return  false
                }

                override fun onResourceReady(
                    resource: Drawable,
                    model: Any,
                    target: Target<Drawable>?,
                    dataSource: DataSource,
                    isFirstResource: Boolean
                ): Boolean {
                    //图片准备完成后可点击
                    holder.itemView.setOnClickListener {
                        imageClick.invoke(holder.adapterPosition)
                    }
                    return  false
                }

            })
            .transform(RoundedCorners(4))
            .into(holder.itemView as ImageView)

    }


    /**
     * itemView:图片
     * width:宽度
     * height:高度
     */
    inner class NinePicsViewHolder(itemView: ImageView,width:Int,height:Int):RecyclerView.ViewHolder(itemView){
        init {
            itemView.layoutParams = ViewGroup.LayoutParams(width,height)
        }
    }


}


sealed class ViewType(val value: Int)
object NONE : ViewType(1)
object NINE : ViewType(2)
object PORTRAIT : ViewType(3) //竖屏
object LANDSCAPE : ViewType(4) //横屏
object VIDEO : ViewType(5)